package com.lonely.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lonely.common.core.domain.auth.UserMsgDto;
import com.lonely.common.enums.CommonStatus;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.order.domain.entity.SysSheinOpenApiSecret;
import com.lonely.order.mapper.SysSheinOpenApiSecretMapper;
import com.lonely.order.service.ISysSheinOpenApiSecretService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: 黄志标
 * @date: 2025/6/20 11:30
 * @description: shein open api密钥服务实现类
 */
@Service
public class SysSheinOpenApiSecretServiceImpl extends ServiceImpl<SysSheinOpenApiSecretMapper, SysSheinOpenApiSecret> implements ISysSheinOpenApiSecretService {


    /**
     * 查询当前登录用户拥有的工厂权限对应的shein接口平台密钥
     *
     * @return
     */
    @Override
    public List<SysSheinOpenApiSecret> queryApiSecretListByLoginUser() {

        UserMsgDto currentLoginUser = SecurityUtils.getLoginUser().getUser();
        if (currentLoginUser.isAdmin()) {
            // 管理员，获取全部密钥
            return queryAll();
        }

        // 获取当前用户对应的openapi 密钥信息
        Set<String> factoryDataPermissions = SecurityUtils.getLoginUser().getFactoryDataPermissions();
        if (CollUtil.isEmpty(factoryDataPermissions)) {
            return new ArrayList<>();
        }

        return super.list(new LambdaQueryWrapper<SysSheinOpenApiSecret>()
                .in(SysSheinOpenApiSecret::getFactoryId, factoryDataPermissions)
                .eq(SysSheinOpenApiSecret::getStatus, CommonStatus.ENABLE.code));
    }

    /**
     * 查询所有shein接口平台密钥
     *
     * @return
     */
    @Override
    public List<SysSheinOpenApiSecret> queryAll() {
        return super.list(new LambdaQueryWrapper<SysSheinOpenApiSecret>().eq(SysSheinOpenApiSecret::getStatus, CommonStatus.ENABLE.code));
    }

    /**
     * 获取指定工厂对应的密钥信息
     *
     * @param factoryId
     * @return
     */
    @Override
    public SysSheinOpenApiSecret getByFactoryId(Long factoryId) {
        return super.getOne(new LambdaQueryWrapper<SysSheinOpenApiSecret>()
                .eq(SysSheinOpenApiSecret::getFactoryId, factoryId)
                .eq(SysSheinOpenApiSecret::getStatus, CommonStatus.ENABLE.code));
    }

    /**
     * 批量获取指定工厂对应的密钥信息
     *
     * @param factoryIds
     * @return
     */
    @Override
    public Map<Long, SysSheinOpenApiSecret> getSheinOpenApiSecretMap(List<Long> factoryIds) {
        if (CollUtil.isEmpty(factoryIds)) {
            return new HashMap<>();
        }
        return super.list(new LambdaQueryWrapper<SysSheinOpenApiSecret>()
                        .in(SysSheinOpenApiSecret::getFactoryId, factoryIds)
                        .eq(SysSheinOpenApiSecret::getStatus, CommonStatus.ENABLE.code))
                .stream().collect(Collectors.toMap(SysSheinOpenApiSecret::getFactoryId, Function.identity()));
    }
}
